import {CHAT_LIST,READ_MSG,READ_CHAt} from '../constant'
import io from 'socket.io-client'
import {reqChatList} from '../../api/'
   //连接服务端
io.socket = io('http://localhost:4000')

  function sendServer(data){
    console.log(data)
    io.socket.emit('sendMsg',data)
 }

export const getMsgList=()=>{
    return (dispatch)=>{
        reqChatList().then((res)=>{
            console.log('异步action',res)
            dispatch(chatlist(res.data))
        })
       
    }
 }
 export const readChat=()=>{
    return dispatch=>{
        io.socket.on('receiveMsg',(data)=>{
            console.log('收到服务发给我的信息',data)
            dispatch(read(data))
        })
    }
 }

 export const sendMsg=(data)=>{
    sendServer(data)    
}

 const chatlist =(data)=>({type:CHAT_LIST,data})
 const read =(data)=>({type:READ_MSG,data})
 export const reads =(data)=>({type:READ_CHAt,data})